<?php

class StringUtil {
    public function explode($template, $s) {
        $arr = explode(",", $s);
        $result = "";
        $i = 0;
        for($i = 0; $i < count($arr); $i++) {
            $result .= str_replace("{0}", $arr[$i], $template);
            $result = str_replace("{1}", urlencode($arr[$i]), $result);
        }

        return $result;
    }

    public static function remove_unicode($str) {
        if(!$str) return false;
        $unicode = array(
        'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
        'd'=>'đ',
        'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
        'i'=>'í|ì|ỉ|ĩ|ị',
        'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
        'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
        'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
        'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Á|Ầ|Ẩ|Ẫ|Ậ',
        'D'=>'Đ',
        'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
        'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
        'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ô|Ớ|Ờ|Ở|Ỡ|Ợ',
        'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
        'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
        );
        foreach($unicode as $nonUnicode=>$uni)
        $str = preg_replace("/($uni)/i",$nonUnicode,$str);

        return $str;
    }

    /**
     * Return part of string. Result will split next space
     *
     * @param string $source
     * @param int $index
     */
    public static function sub_string($source_string, $index) {
        if (strlen($source_string) <= $index)
            return $source_string;
        $source = $source_string;
        $secondStr = substr($source, 0 , $index);
        if (strpos($secondStr, " ")) {
            $newIdx = strrpos($secondStr, " ");
        } else {
            $newIdx = strlen($secondStr);
        }
        $result = substr($source, 0, $newIdx);

        if (count(explode(" ",trim($result))) == 1) {
            $source = $result;
            $source = StringUtil::insert_string($source, " ", 15);
            $secondStr = substr($source, 0, $index);
            if (strpos($secondStr, " ")) {
                $newIdx = strrpos($secondStr, " ");
            } else {
                $newIdx = strlen($secondStr);
            }
            $result = substr($source, 0, $newIdx);
        }

        if (strlen($result) < strlen($source_string))
            $result .= "...";
        return $result;
    }

    public static function  insert_string($old_string, $insert_string, $pos) {
        $new = $old_string;
        $str1 = substr($new, 0 , $pos);
        $str2 = substr($new, $pos);
        while (strlen($str2) > $pos) {
            $str1 .= $insert_string . substr($str2, 0, $pos);
            $str2 = substr($str2, $pos);
        }
       return $str1 .$insert_string . $str2;
    }

    public static function strnpos($source, $sub_str, $n) {
        if ($n <= 0 || intval($n) != $n || substr_count($source, $sub_str) < $n)
            return FALSE;

        $sub_str = strval($sub_str);
        $len = 0;

        for ($i=0 ; $i<$n-1 ; ++$i) {
            if ( strpos($source, $sub_str) === FALSE )
                return FALSE;
            $len += strlen( substr($source, 0, strpos($source, $sub_str) + strlen($sub_str)) );
            $source = substr($source, strpos($source, $sub_str) + strlen($sub_str) );
        }
        return strpos($source, $sub_str) + $len;
    }
}

function get_more_close_tag($string,$num=20, $start = 0)
{
	$string=str_replace('&nbsp;',' ',$string);
	$output=$string;
	$match=array();
	$match1=array();
	$match2=array();

	if(strlen($string) > $num)
    {
    	$pt='/<[^>]*>/i';
    	//match all tag in string
		preg_match_all($pt,$string,$match);

	   	$count=0;
		$pos=0;
		$match_pos=0;
		//count $num character in string have tag, and find it position
		while($pos<strlen($string))
		{
			if($count==$num)
				break;
			if($string[$pos]!='<')
			{
				$count++;
				$pos+=1;
			}
			if($string[$pos]=='<' && $string[$pos+strlen($match[0][$match_pos])-1]=='>')
			{
				$pos+=strlen($match[0][$match_pos]);
				$match_pos++;
			}


		}

		$output=substr($string,$start,$pos-1);
		$output .= ' ...';

		$pattern1='/(<[^>\/]*>)/i';
		$pattern2='/<[\/]+[^>]*>/i';

		$count1=preg_match_all($pattern1,$output,$match1);//tag open
		$count2=preg_match_all($pattern2,$output,$match2);//tag closed

		$not_close_tag=$count1-$count2;
		//close tag if it not
		for($i=sizeof($match1[0])-sizeof($match2[0])-1;$i>=0;$i--)
		{
			$temp_string=drupal_substr($match1[0][$i],1);
			$tag_need_to_close='</'.$temp_string;
			$output.=$tag_need_to_close;
		}

	}

	$pt='/<[^>]*>/i';
	preg_match_all($pt,$output,$match);
	$word = array();
    $temp_output=str_replace($match[0],'<tag>',$output);

    $word=explode('<tag>',$temp_output);
    $temp_output=implode(' ',$word);

    $word=explode('&nbsp;',$temp_output);
    $temp_output=implode(' ',$word);

    $long_word=array();
    $count_long_word_replacement=0;

    $word = explode(' ',$temp_output);
    foreach ($word as $value)
    {
    	if (strlen($value)>22)
    	$long_word[]=$value;
    }
    $long_word_replacement=array();
    $long_word_replacement=$long_word;
    foreach ($long_word_replacement as &$value1)
    {
    	$value1 = wordwrap($value1,15," ", true);
    }
    foreach ($long_word as $value2)
    {
    	$output=str_replace($value2,$long_word_replacement[$count_long_word_replacement],$output);
     	$count_long_word_replacement++;
    }
    $output=str_replace('</br>',' ',$output);
 	/*echo '<pre>';
  	print_r($output).'<br>';
   	//print_r($output).'<br>';
   	echo '</pre>';
   	exit();*/

    return $output;
}